#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpf_datasource_v1.py 
@version:
@time: 2022/03/11 
"""
from TapClientCore.T06_Tapdata_feature.v2.TPF01_datasource.tpf_datasource_base import V2PFDataSourceBase, logger
from TapException.TM import *


class V2TPFDataSource(V2PFDataSourceBase):

    def create_datasource_by_db_info(self, db_info):
        name = db_info.get('name')
        connection_id = None
        tm_response = None
        mongodb_response = []
        # 查询TM
        try:
            tm_response = self.TMFHTTP.pdk_query_by_name(name)
        except TapdataNoThisDataSource:
            tm_response = {}
        # 查询中间库
        mongodb_response = self.datasource_query_mongodb(db_info)
        # insert to 中间库
        # TM 0，中间库 1
        # create， update中间信息
        connection_id = tm_response.get('id')

        if not mongodb_response:
            # 中间库 0
            if not tm_response:
                # TM 0, 中间库 0 create
                create_response = self.TMFHTTP.pdk_create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                # TM 1，中间库 0
                self.datasource_insert_mongodb(db_info, connection_id)
        else:
            # 中间库 1
            if not tm_response:
                # TM 0， 中间库 1 delete ,insert
                self.datasource_delete_mongodb(db_info)
                create_response = self.TMFHTTP.pdk_create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                # TM 1, 中间库 1
                if connection_id == mongodb_response.get('id'):
                    # self.load_datasource_schema_by_db_info(db_info)
                    pass
                else:
                    self.datasource_delete_mongodb(db_info)
                    self.datasource_insert_mongodb(db_info, connection_id)
        return connection_id

    def update_datasource_by_db_info(self, db_info, update_attr):
        name = db_info.get('name')
        connection_id = None
        tm_response = None
        mongodb_response = []
        # 查询TM
        tm_response = self.TMFHTTP.pdk_query_by_name(name)
        # 查询中间库
        mongodb_response = self.datasource_query_mongodb(db_info)

        tmp = db_info.copy()
        db_info.update(update_attr)

        mongodb_response2 = self.datasource_query_mongodb(db_info)
        connection_id = tm_response.get('id')
        # 更新逻辑
        if not mongodb_response:
            # 中间库 0
            if not tm_response:
                # TM 0, 中间库 0 create
                create_response = self.TMFHTTP.pdk_create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                # TM 1，中间库 0
                self.TMFHTTP.patch_data_source_by_id(connection_id, db_info)
                self.datasource_insert_mongodb(db_info, connection_id)
        else:
            # 中间库 1
            if not tm_response:
                # TM 0， 中间库 1 delete ,insert
                self.datasource_delete_mongodb(db_info)
                create_response = self.TMFHTTP.pdk_create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                # TM 1, 中间库 1
                if connection_id == mongodb_response.get('id'):
                    self.TMFHTTP.patch_data_source_by_id(connection_id, db_info)
                    self.datasource_update_mongodb(tmp, connection_id, db_info)
                else:
                    self.datasource_delete_mongodb(tmp)
                    self.TMFHTTP.patch_data_source_by_id(connection_id, db_info)
                    self.datasource_insert_mongodb(db_info, connection_id)

    def delete_datasource_by_db_info(self, db_info):
        self.TMFHTTP.pdk_delete_datasource(db_info.get('name'))
        self.datasource_delete_mongodb(db_info)

    # load schema

    def load_datasource_schema_by_db_info(self, db_info):
        """
        only ws加载schema
        :param db_info:
        :return:
        """
        name = db_info.get('name')
        self.load_datasource_schema(name)

    def datasource_test_by_db_info(self, db_info):
        """
        测试连接+http+ws加载schema
        :param db_info:
        :return:
        """
        name = db_info.get('name')
        self.datasource_test(name)
